package cn.bdqn.asecu.controller;

import cn.bdqn.asecu.service.AsecuLogService;
import cn.bdqn.pojo.Operationlog;
import cn.bdqn.pojo.Project;
import cn.bdqn.pojo.User;
import cn.bdqn.pojo.Worklog;
import cn.bdqn.vo.UserWorkLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 日志
 * @author 8
 */
@Controller
public class LogController {

    @Autowired
    AsecuLogService asecuLogService;


    /**
     * 操作日志页面
     * @param model
     * @return
     */
    @RequestMapping("/systemLog")
    public String systemLog(Model model){
        List<Operationlog> systemLogs = asecuLogService.getSystemLog();
        model.addAttribute("systemLogs",systemLogs);
        return "/system-log";
    }


    /**
     * 工作日志页面
     * @param model
     * @return
     */
    @RequestMapping("/workLog")
    public String workLog(Model model,HttpServletRequest request,String project,Date workdate){
        User user= (User) request.getSession().getAttribute("user");
        if (project!=null) {
            if (StringUtils.isBlank(project)){
                project=null;
            }
        }
        List<UserWorkLog> workLogs = asecuLogService.getWorkLog(user.getId(),project,workdate);
        model.addAttribute("workLogs",workLogs);
        model.addAttribute("project",project);
        model.addAttribute("workdate",workdate);
        return "/work-log";
    }

    /**
     * 工作日志添加页面
     * @return
     */
    @RequestMapping("/worklog-add")
    public String workLogHtml(Model model){
        List<Project> projects = asecuLogService.getProject();
        model.addAttribute("projects",projects);
        return "/worklog-add";
    }

    /**
     * 添加工作日志
     * @param worklog
     * @param request
     */
    @RequestMapping("/addWorkLog")
    @ResponseBody
    public Map addWorkLog(Worklog worklog, HttpServletRequest request){
        User user= (User) request.getSession().getAttribute("user");
        worklog.setUserId(user.getId());
        worklog.setUptime(new Date());
        Map map= new HashMap<>();
        boolean b = asecuLogService.addWorkLog(worklog);

        if (b){
            map.put("status",200);
        }else {
            map.put("error","添加失败");
        }
        return map;
    }

    /**
     * 删除工作日志
     * @param ids
     * @return
     */
    @RequestMapping("/delWorkLog")
    @ResponseBody
    public String delWorkLog(Integer[] ids){
        asecuLogService.delWorkLog(ids);
        return "200";
    }

    /**
     * 查看工作日志
     * @param id
     * @return
     */
    @RequestMapping("/worklog_view")
    public String worklog_view(Integer id,Model model){
        Worklog worklog = asecuLogService.worklog_view(id);
        model.addAttribute("worklog",worklog);
        return "worklog-view";
    }
    /**
     * 编辑工作日志页面
     * @param id
     * @return
     */
    @RequestMapping("/worklog_edit")
    public String worklog_edit(Integer id,Model model){
        Worklog worklog = asecuLogService.worklog_view(id);
        List<Project> projects = asecuLogService.getProject();
        model.addAttribute("projects",projects);
        model.addAttribute("worklog",worklog);
        return "worklog-edit";
    }

    /**
     * 编辑工作日志
     * @param worklog
     * @return
     */
    @RequestMapping("/editWorklog")
    @ResponseBody
    public String editWorklog(Worklog worklog,HttpServletRequest request){
        User user= (User) request.getSession().getAttribute("user");
        worklog.setUptime(new Date());
        worklog.setUserId(user.getId());
        Worklog worklog1 = asecuLogService.saveWorkLog(worklog);
        if (worklog1!=null){
            return "200";
        }
        return "error";
    }
}


